package com.example.teaching_server.repository;

import com.example.teaching_server.entity.ScheduleEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.time.LocalDate;
import java.util.List;

/**
 * 课程表数据访问层
 */
@Repository
public interface ScheduleRepository extends JpaRepository<ScheduleEntity, Long> {

    /**
     * 检查特定日期、时间段和班级的排课是否存在
     */
    @Query("SELECT COUNT(s) > 0 FROM ScheduleEntity s WHERE " +
            "s.specificDate = :specificDate AND " +
            "s.timeSlot.slotId = :timeSlotId AND " +
            "s.classEntity.id = :classId")
    boolean existsBySpecificDateAndTimeSlotAndClass(
            @Param("specificDate") LocalDate specificDate,
            @Param("timeSlotId") Long timeSlotId,
            @Param("classId") Long classId);

    /**
     * 检查特定日期、时间段和教室的排课是否存在
     */
    @Query("SELECT COUNT(s) > 0 FROM ScheduleEntity s WHERE " +
            "s.specificDate = :specificDate AND " +
            "s.timeSlot.slotId = :timeSlotId AND " +
            "s.classroom.id = :classroomId")
    boolean existsBySpecificDateAndTimeSlotAndClassroom(
            @Param("specificDate") LocalDate specificDate,
            @Param("timeSlotId") Long timeSlotId,
            @Param("classroomId") Long classroomId);

    /**
     * 检查特定日期、时间段和教师的排课是否存在
     */
    @Query("SELECT COUNT(s) > 0 FROM ScheduleEntity s WHERE " +
            "s.specificDate = :specificDate AND " +
            "s.timeSlot.slotId = :timeSlotId AND " +
            "s.teacher.teacherId = :teacherId")
    boolean existsBySpecificDateAndTimeSlotAndTeacher(
            @Param("specificDate") LocalDate specificDate,
            @Param("timeSlotId") Long timeSlotId,
            @Param("teacherId") Long teacherId);

    /**
     * 根据教师、班级和日期范围查询排课
     */
    @Query("SELECT s FROM ScheduleEntity s WHERE " +
            "s.teacher.teacherId = :teacherId AND " +
            "s.classEntity.id = :classId AND " +
            "s.specificDate BETWEEN :startDate AND :endDate " +
            "ORDER BY s.specificDate, s.timeSlot.sortOrder")
    List<ScheduleEntity> findByTeacherClassAndDateRange(
            @Param("teacherId") Long teacherId,
            @Param("classId") Long classId,
            @Param("startDate") LocalDate startDate,
            @Param("endDate") LocalDate endDate);

    /**
     * 根据班级ID和日期范围查询排课
     */
    @Query("SELECT s FROM ScheduleEntity s WHERE s.classEntity.id = :classId " +
            "AND s.specificDate BETWEEN :startDate AND :endDate " +
            "ORDER BY s.specificDate, s.timeSlot.sortOrder")
    List<ScheduleEntity> findByClassIdAndDateRange(@Param("classId") Long classId,
                                                   @Param("startDate") LocalDate startDate,
                                                   @Param("endDate") LocalDate endDate);

    /**
     * 查询所有排课
     */
    List<ScheduleEntity> findAll();

    /**
     * 根据教师ID查询排课
     */
    List<ScheduleEntity> findByTeacherTeacherId(Long teacherId);

    /**
     * 根据班级ID查询排课
     */
    List<ScheduleEntity> findByClassEntityId(Long classId);
}
